<script>
	import Vue from 'vue'
	import chat from 'model/chatModel.js'
	import {appConfig} from 'model/commonModel.js'
	import {mapState,mapMutations} from 'vuex';
	let disp = require("common/broadcast");
	let me = null;
	export default {
		onLaunch: function() {
			console.log('App onLaunch...')
			//#ifdef APP-PLUS
			//plus.navigator.setFullscreen(true);
			// #endif
			me = this;
			Vue.prototype.systemInfo = uni.getSystemInfoSync();
			// console.log(JSON.stringify(Vue.prototype.systemInfo));
			Vue.prototype.StatusBar = Vue.prototype.systemInfo.statusBarHeight;
			Vue.prototype.SafeAreaHeight = 0;
			
			Vue.prototype.WindowHeight = Vue.prototype.systemInfo.screenHeight;
			Vue.prototype.WindowHeight = Vue.prototype.systemInfo.windowHeight<Vue.prototype.systemInfo.screenHeight?Vue.prototype.systemInfo.windowHeight:Vue.prototype.systemInfo.screenHeight;
			
			if(Vue.prototype.systemInfo.safeArea){
				Vue.prototype.SafeAreaHeight = Vue.prototype.WindowHeight-Vue.prototype.systemInfo.safeArea.bottom;
			}
			
			me['baseModule/setWinHeight'](Vue.prototype.WindowHeight);
			Vue.prototype.NativeBar = 0;
			
			var navHeight = uni.upx2px(100)+Vue.prototype.SafeAreaHeight/1.5;
			me['baseModule/setNavHeight'](navHeight);
			me.setStore('navHeight', navHeight);
			
			// #ifdef H5
			Vue.prototype.Custom = {backButtonRight:0};
			// #endif
			
			// #ifndef MP
			if (Vue.prototype.systemInfo.platform == 'android') {
				Vue.prototype.CustomBar = Vue.prototype.systemInfo.statusBarHeight + 50;
			} else {
				Vue.prototype.CustomBar = Vue.prototype.systemInfo.statusBarHeight + 45;
			};
			
			// #endif
			
			// #ifdef MP-WEIXIN
			Vue.prototype.StatusBar = Vue.prototype.systemInfo.statusBarHeight;
			let custom = uni.getMenuButtonBoundingClientRect();
			Vue.prototype.Custom = custom;
			Vue.prototype.Custom.backButtonRight = 0;
			Vue.prototype.CustomBar = custom.bottom + custom.top - Vue.prototype.systemInfo.statusBarHeight;
			// #endif		
			
			Vue.prototype.NativeBar = Vue.prototype.CustomBar;
			
			this['baseModule/setStatusBarHeight'](Vue.prototype.StatusBar);
			this['baseModule/setBarHeight'](Vue.prototype.CustomBar);
			this['baseModule/setNativeBarHeight'](Vue.prototype.NativeBar);
			
			//#ifdef APP-PLUS
			plus.screen.lockOrientation('portrait-primary'); //锁定屏幕方向
			// #endif
			if(me.getStore('navHeight')){
				me['baseModule/setNavHeight'](me.getStore('navHeight'));
			}
			me.getAppConfig();
			//读取个人信息
			me.userInfoSync();
			// me.socketConnect();
			

			Vue.prototype.loginOut = function(){
				me['userModule/logout']();
			};
			disp.on("refreshConfig", function() {
				me.getAppConfig();
			});
			
		},
		onShow: function() {
			console.log('App onShow...')
		},
		onHide: function() {
			console.log('App onHide...')
		},
		computed: {
			...mapState({
				userInfo: ({
					userModule
				}) => userModule.userInfo,
				hasLogin: ({
					userModule
				}) => userModule.hasLogin,
				token: ({
					chatModule
				}) => chatModule.token,
				historyMsgList: ({
					chatModule
				}) => chatModule.historyMsgList,
				unreadMsgCount: ({
					chatModule
				}) => chatModule.unreadMsgCount,
				chatWindowsId: ({
					chatModule
				}) => chatModule.chatWindowsId,
				winHeight: ({
					baseModule
				}) => baseModule.winHeight,
				orderAccepter: ({
					userModule
				}) => userModule.orderAccepter,
			})
		},
		watch: {
			hasLogin(val) {
				if (val) {
					me.socketConnect();
					chat.initLocalData(me);
					me.loginToChat();
				}
				else{
					me.socketClose();
				}
			}
		},
		methods: {
			...mapMutations([
				'baseModule/setAppConfig',
				'baseModule/setNavHeight',
				'userModule/login', 
				'userModule/logout', 
				'baseModule/setMsgBadge',
				'baseModule/setWinHeight',
				'baseModule/setNavHeight', 
				'baseModule/setWinWidth', 
				'baseModule/setBarHeight', 
				'baseModule/setStatusBarHeight',
				'baseModule/setNativeBarHeight',
				'chatModule/setToken',
				'chatModule/setHistoryMsgList',
				'chatModule/setUnreadMsgCount'
			]),
			getAppConfig(){
				appConfig(me).then(res=>{me['baseModule/setAppConfig'](res);}).catch(err=>{});
			},
			userInfoSync() {
				var info = this.getUserInfo();
				if (info) {
					this['userModule/login'](info);
				}
			},
			socketConnect() {
				if(me.chatSocket){
					me.chatSocket.close();
					me.chatSocket = null;
				}
				
				me.chatSocket = uni.connectSocket({
					url: me.WssUrl,
					success: (res) => {
						console.log('App.vue print -> WebSocket创建成功.');
					},
					fail: (res) => {
						console.log('App.vue print -> WebSocket创建失败.');
					}
				});
				me.chatSocket.onOpen(function(res) {
					console.log('App.vue print -> WebSocket连接已打开.');
					clearInterval(me.reConnectIntervalID);
					me.reConnectIntervalID = 0;
					me.heartBeat();
					me.loginToChat();
				});
				me.chatSocket.onClose(function(res) {
					console.log('App.vue print -> WebSocket连接已关闭.');
					if (me.reConnectIntervalID == 0) {
						me.reSocketConnect();
					}
				});
				me.chatSocket.onError(function(res) {
					console.log('App.vue print -> WebSocket连接打开失败.');
					if (me.reConnectIntervalID == 0) {
						me.reSocketConnect();
					}
				});
				me.chatSocket.onMessage(function(res) {
					var msg = JSON.parse(res.data);
					if(msg.message_type=="quit"){
						me.setUserInfo(null);
						me['userModule/logout']();
						me.chatSocket.close();
						me.chatSocket = null;
					}
					else{
						chat.onMessage(me, res);
						if(msg.message_type!="pong"){
							me.sendHeartbeat();
						}
						
					}
				});
			},
			reSocketConnect() {
				console.log('App.vue print -> WebSocket开始重新连接.');
				clearInterval(me.chatHeartbeatIntervalID);
				me.chatHeartbeatIntervalID = 0;
				if (me.reConnectIntervalID == 0) {
					me.reConnectIntervalID = setInterval(me.socketConnect, 2000);
				}
			},
			sendHeartbeat() {
				me.chatSocket.send({
					data: JSON.stringify({
						type: 'ping',
						platform: this.systemInfo.uniPlatform,
					})
				});
			},
			socketClose() {
				if(me.chatSocket){
					clearInterval(me.reConnectIntervalID);
					me.reConnectIntervalID = 0;
					clearInterval(me.chatHeartbeatIntervalID);
					me.chatHeartbeatIntervalID = 0;
					me.chatSocket.close();
					me.chatSocket = null;
				}
			},
			loginToChat() {
				if(me.hasLogin){
					me.chatInit();
				}
			},
			chatInit() {
				me.chatSocket.send({data: JSON.stringify({type: 'init'})});
			},
			heartBeat(){
				me.chatHeartbeatIntervalID = setInterval(me.sendHeartbeat, 13000);
			}
		}
	}
</script>


<style>
	/* #ifndef APP-PLUS-NVUE */
	@import "colorui/main.css";
	@import "colorui/icon.css";
	@import "App.css";
	/* #endif */
</style>
